using System.Collections.Generic;
using UnityEngine;

public class Skill_DomainExpansion : Skill_Base
{

    [Header("----- Domain Expand Details -----")]
    [SerializeField] private GameObject domain;
    public float expandSpeed = 3f;
    public float domainExpandFactor = 6f;

    [Header("----- Slowdown Upgrade -----")]
    [SerializeField] private float slowdownPecent = .8f;
    [SerializeField] private float slowdownDuration = 5f;

    [Header("-----Spell Casting Upgrade -----")]
    [SerializeField] private float spellCastingSlowdownPecent = .9f;
    [Space]
    [SerializeField] private float spellCastShardDuration = 6f;
    [SerializeField] private int spellCastShardVolume = 15;
    [Space]
    [SerializeField] private float spellCastEchoDuration = 5f;
    [SerializeField] private int spellCastEchoVolume = 10;
    private List<Enemy> targetEnemys = new();
    private Transform currentTarget;
    private float spellCastInterval;
    private float spellCastTimer;
    public override bool CanUseSkill()
    {
        if (base.CanUseSkill() == false)
            return false;
        if (player.stateMachine.currentState == player.wallSildeState)
            return false;
        return true;
    }

    public void CreateDomainExpansion()
    {
        spellCastInterval = GetDomainDuration() / GetSpellCastVolume();
        GameObject domainPrefab = Instantiate(domain, transform.position, Quaternion.identity);
        domainPrefab.GetComponent<SkillObject_Domain>().SetUpDomain(this);
    }
    
    public void SpellCast()
    {
        spellCastTimer -= Time.deltaTime;
        if (currentTarget == null)
            currentTarget = FindTargetInDomain();
        if (currentTarget != null && spellCastTimer <= 0)
        {
            spellCastTimer = spellCastInterval;
            CreateDomain(currentTarget);
            currentTarget = null;
        }
    }

    private void CreateDomain(Transform target)
    {
        if (Unlock(SkillUpgradeType.Domain_EchoSpam))
        {
            Vector3 offset = UnityEngine.Random.value < .5f ? new Vector3(1, 0, 0) : new Vector3(-1, 0, 0);
            skillManager.skillTimeEcho.CreateTimeEcho(target.position + offset);
        }
        else if (Unlock(SkillUpgradeType.Domain_ShardSpam))
            skillManager.skillShard.CreateRawShard(3, target, true);
    }

    public void CollectEnemyInDomain(Enemy enemy) => targetEnemys.Add(enemy);

    public void ClearCollectedEnemy()
    {
        foreach (var enemy in targetEnemys)
            enemy.StopSlowdownEntityCo();
        targetEnemys.Clear();
    }

    private Transform FindTargetInDomain()
    {
        targetEnemys.RemoveAll(target => target == null || target.enemyHealth.isDead);

        if (targetEnemys.Count == 0)
            return null;

        int index = UnityEngine.Random.Range(0, targetEnemys.Count);

        return targetEnemys[index].transform;
    }

    public bool CanMoveToSky()
    {
        if (Unlock(SkillUpgradeType.Domain_Slowdown))
            return false;
        return true;
    }

    public float GetSpellCastVolume()
    {
        if (Unlock(SkillUpgradeType.Domain_ShardSpam))
            return spellCastShardVolume;
        else if (Unlock(SkillUpgradeType.Domain_EchoSpam))
            return spellCastEchoVolume;
        return 0;

        
    }
   
    public float GetSlowdownPercent()
    {
        if (Unlock(SkillUpgradeType.Domain_Slowdown))
            return slowdownPecent;
        return spellCastingSlowdownPecent;
    }

    public float GetDomainDuration()
    {
        if (Unlock(SkillUpgradeType.Domain_Slowdown))
            return slowdownDuration;
        if (Unlock(SkillUpgradeType.Domain_ShardSpam))
            return spellCastShardDuration;
        return spellCastEchoDuration;
    }
    
}
